package com.neasho.neashoadminv1.infrastructure.domain.generator

import cn.hutool.core.util.StrUtil

class AppTableView {
    var tableSchema: String? = null
    var tableName: String? = null
    var tableComment: String? = null
    var columns: MutableList<AppTableColumn> = mutableListOf()

    /**
     * 包路径中的前缀符
     */
    val packagePrefix:String
        get() = tableName?.split("_")[0]?.lowercase()?:"dft"

    /**
     * 类名
     */
    val upperName:String
        get() = StrUtil.upperFirst(StrUtil.toCamelCase(tableName))

    val varName: String
        get() = StrUtil.lowerFirst(StrUtil.toCamelCase(tableName))

    val permissionPrefix: String
        get() = tableName?.replace("_", ":")?:""

    val urlPrefix: String
        get() = tableName?.replace("_", "/") ?: ""

    val fieldsWithoutId:List<AppTableColumn>
        get() = columns.filter { it.columnKey != "PRI" }

    val namesLikeFields:List<AppTableColumn>
        get() = columns.filter { it.columnName.lowercase().endsWith("name") }

    val enumList:List<AppTableColumn>
        get() = columns.filter { it.columnType.startsWith("enum") }

    companion object {
        fun of(appTable: AppTable) :AppTableView {
            return AppTableView().also {
                it.tableSchema = appTable.tableSchema
                it.tableName = appTable.tableName
                it.tableComment = appTable.tableComment
            }
        }
    }
}